// 服务
//get
//post
app.factory("httpService",["$http","$rootScope","$localStorage","$location","toaster",function($http,$rootScope,$localStorage,$location,toaster){var doRequest=function(type,path,params,success_callback,error_callback,isNeedLoad){var url;/*if(isNeedLoad == undefined){
             isNeedLoad = true; //是否显示加载中 图标
             }
             if(!$rootScope.isLoading && isNeedLoad){
             //开启加载图标
             $rootScope.isLoading = true;
             }*/
return url="qiniu"==path?params?CONFIG.qiniu_tokenPath+"?key="+params:CONFIG.qiniu_tokenPath:CONFIG.basePath+path,path+=path.indexOf("?")>=0?"&random="+Math.random():"?random="+Math.random(),$http({method:type,data:params,url:url,headers:{"Content-Type":"application/json"}}).success(function(data){data.success?success_callback&&success_callback(data):error_callback?error_callback(data.message):toaster.error("错误",data.message)}).error(function(error){
//关闭加载图标
/*$rootScope.isLoading = false;
                 */
error_callback?error_callback(error):toaster.error("错误",error.Message)})};return{request:function(type,path,params,success_callback,error_callback,isNeedLoad){return doRequest(type,path,params,success_callback,error_callback,isNeedLoad)}}}]),
//http 拦截器
app.factory("HttpInterceptor",["$q","$localStorage","$location","$timeout",function($q,$localStorage,$location,$timeout){return{
// 请求发出之前，可以用于添加各种身份验证信息
request:function(config){return $localStorage.token&&(config.headers.Authorization="bearer "+$localStorage.token),config},
// 请求发出时出错
requestError:function(err){return $q.reject(err)},
// 成功返回了响应
response:function(res){return res},
// 返回的响应出错，包括后端返回响应时，设置了非 200 的 http 状态码
responseError:function(err){return err.Message="",401===err.status?(console.log(" 401无权限 ，进行跳转至登录界面 "),err.Message=" 401无权限 ，进行跳转至登录界面 ",$timeout(function(){$location.path("/login")},2e3)):500===err.status&&(console.log(" 500 ，服务器错误，请联系管理员！ "),err.Message=" 500 ，服务器错误，请联系管理员！ "),$q.reject(err)}}}]),
//获取qinniu的token
app.service("QiniuToken",function(httpService,$localStorage){this.getToken=function(callBack){httpService.request("get","qiniu",null,function(result){$localStorage.qiniuToken=result.data.Token,$localStorage.Domain=result.data.Domain+"/",callBack&&callBack()})},this.delImg=function(key,callBack){httpService.request("get","qiniu",key,function(result){callBack&&callBack()})}});